var calendar;
var day;
var month;
var year;
var isNew;
var dayEvent;
$(function() {
		var date = new Date();
		var y = date.getFullYear();
		$('#history').hide();
		$(".tree").treeview({animated: "medium"});
		$( "#datepicker_start" ).datepicker();
		$( "#datepicker_end" ).datepicker();
		$('#history_tab').click(function() {
  			$('#modifyForm').hide();
			$('#submit').hide();
			$('#history').show();
			$('#Item_tab').removeClass('current'); 
			$('#history_tab').addClass('current'); 
		});
		
		$('#Item_tab').click(function() {
  			$('#modifyForm').show();
			$('#submit').show();
			$('#history').hide();
			$('#Item_tab').addClass('current'); 
			$('#history_tab').removeClass('current'); 
		});
		

	});
	
function showCalendar(enableModify) {
	var startDate =$("#datepicker_start").val();

	if (startDate != "") {
		if (calendar == null) {
			createCalendar(enableModify);
			var stratDateArray = startDate.split("/");
			calendar.fullCalendar("gotoDate",stratDateArray[2],stratDateArray[0]-1);
			$("#calendar").css('display','none');
		}
		if ($("#datepicker_end").val() != "") {
			$("#calendar").css('display','');
		}
	}
}

function createCalendar(enableModify) {
	calendar = $('#calendar').fullCalendar({
		height: 220,
		theme: true,
		selectHelper: false,
		allDayDefault: true,
		dayClick: function(selectDate, allDay,jsEvent,view){
					clickDay(selectDate, enableModify);	
		},
		eventClick: function(calEvent, jsEvent, view) {
					clickEvent(calEvent);
		},
					selectable: true,
	});

}

function selectEvent() {
  var e = $("#event").val();
  			if (e == "") {
				//delete event
				calendar.fullCalendar('removeEvents', dayEvent.id);
			} else  if (isNew) {
				//create event
				calendar.fullCalendar('renderEvent', {
								id:year+month+day,
								title: e,
								start: new Date(year, month, day),
								allDay: true,
				},
				true ) 
			} else {
				//update event
				dayEvent.title = e;
				calendar.fullCalendar('updateEvent', dayEvent);
			
			}
			calendar.fullCalendar('unselect');
			$("#event_tab").css('display','none');
}

function cancelEventButton() {
	$("#event_tab").css('display','none');
}


function historyGroup(id) {
	var display = document.getElementById("history_"+id).style.display;
	if (display == 'none') {
		$("#history_"+id).css('display','');
		$("#tree_"+id).replaceWith("<img src='../../../images/down.png' width='12' height='12' id='tree_"+id+"'/>");
	} else {
		document.getElementById("history_"+id).style.display = "none";
		$("#tree_"+id).replaceWith("<img src='../../../images/right.png' width='12' height='12' id='tree_"+id+"'/>");
	}
	
}

function clickDay(selectDate, enableModify) {
		var endDate = 	$("#datepicker_end").val().split("/");
		var startDate1 =$("#datepicker_start").val().split("/");
		
		var selectStartDate = new Date(startDate1[2],startDate1[0]-1,startDate1[1]);
		var selectEndDate = new Date(endDate[2],endDate[0]-1,endDate[1]);
		if (selectStartDate <= selectDate && selectDate <= selectEndDate && enableModify) {
			$("#event_tab").css('display','');
			month = selectDate.getMonth();
			day = selectDate.getDate();
			year =selectDate.getFullYear();
			isNew = true;
			
			$("#event_title").replaceWith('<div id="event_title">'+(selectDate.getMonth()+1)+'/'+selectDate.getDate()+'/'+selectDate.getFullYear()+'</div>');
			
			if (selectDate.getDay()==0 || selectDate.getDay()==6) {
				document.getElementById("event").value = "Non-Working Day";	
			} else {
				document.getElementById("event").value = "Working Day";	
			}
		} else {
			$("#event_tab").css('display','none');
		}
}

function clickEvent(calEvent) {
		selectedDate = calEvent.start;
		month = selectedDate.getMonth();
		day = selectedDate.getDate();
		year =selectedDate.getFullYear();
		isNew = false;
		dayEvent = calEvent;
		$("#event_title").replaceWith('<div id="event_title">'+(month+1)+'/'+day+'/'+year+'</div>');
		$("#event_tab").css('display','');
	}